package com.frogs.ucenter.v1.service.impl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.frogs.framework.base.service.daoimpl.BaseServiceImpl;
import com.frogs.framework.util.PageList;
import com.frogs.system.web.api.request.Request;
import com.frogs.system.web.api.response.Response;
import com.frogs.system.web.exception.ServiceOperationException;
import com.frogs.ucenter.dao.LifeWayDao;
import com.frogs.ucenter.entity.LifeWay;
import com.frogs.ucenter.v1.request.LifeWayListRequest;
import com.frogs.ucenter.v1.request.LifeWayMergeRequest;
import com.frogs.ucenter.v1.response.AddResponse;
import com.frogs.ucenter.v1.response.ListResponse;
import com.frogs.ucenter.v1.service.LifeWayService;
import com.frogs.ucenter.v1.util.ApiConstants;

@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class) 
@Service(ApiConstants.VERSION+"lifeWayService")
public class LifeWayServiceImpl extends BaseServiceImpl<LifeWay> implements LifeWayService{

	private static final Logger log = LoggerFactory.getLogger(LifeWayServiceImpl.class);

	@Resource
	private LifeWayDao lifeWayDao;
	
	@Override
	public Response mergeRecord(Request request) throws Exception{
		AddResponse<LifeWay> response = new AddResponse<LifeWay>();
		try {
			LifeWayMergeRequest lwMergeRequest = (LifeWayMergeRequest)request; 
			LifeWay lifeWay = new LifeWay();
			Integer id = lwMergeRequest.getId();
			lifeWay.setUserId(lwMergeRequest.getUserId());
			lifeWay.setSportRate(lwMergeRequest.getSportRate());
			lifeWay.setSmoking(lwMergeRequest.getSmoking());
			lifeWay.setDrinkWine(lwMergeRequest.getDrinkWine());
			lifeWay.setDrinkWater(lwMergeRequest.getDrinkWater());
			lifeWay.setEatFruit(lwMergeRequest.getEatFruit());
			lifeWay.setEatVegetable(lwMergeRequest.getEatVegetable());
			lifeWay.setEatRoughage(lwMergeRequest.getEatRoughage());
			lifeWay.setTaste(lwMergeRequest.getTaste());
			lifeWay.setMealRule(lwMergeRequest.getMealRule());
			lifeWay.setRiseTime(lwMergeRequest.getRiseTime());
			lifeWay.setSleepTime(lwMergeRequest.getSleepTime());
			lifeWay.setNoonBreak(lwMergeRequest.getNoonBreak());
			
			if(id==0){
				lifeWayDao.insert(lifeWay);
				response.setInfo(lifeWay);
				response.setTip("添加生活方式档案成功");
			}else{
				lifeWay.setId(id);
				lifeWayDao.update(lifeWay);
				response.setInfo(lifeWay);
				response.setTip("修改生活方式档案成功");
			}
			
		} catch (Exception e) {
			log.error("func[{}] request[{}] exception[{}] desc[fail]",
					new Object[] { this.getClass().getName() +"."+Thread.currentThread().getStackTrace()[1].getMethodName(),
						request,Arrays.deepToString(e.getStackTrace()) });
			throw new ServiceOperationException("请求处理失败：" + e.getMessage());
		}
		return response;
	}

	@Override
	public Response listRecord(Request request) throws Exception{
		ListResponse<LifeWay> response = new ListResponse<LifeWay>();
		try {
			LifeWayListRequest lwListRequest = (LifeWayListRequest)request;
			Integer userId = lwListRequest.getUserId();
			Integer pageNo = lwListRequest.getPageNo();
			Integer pageSize = lwListRequest.getPageSize();
			
			Map<String,Object> params = new HashMap<String,Object>();
			params.put("eqUserId", userId);
			PageList<LifeWay> list = new PageList<LifeWay>();
			list = lifeWayDao.selectByMap(params, pageNo, pageSize);
			response.setList(list);
			response.setTip("生活方式档案查看成功");
			
		} catch (Exception e) {
			log.error("func[{}] request[{}] exception[{}] desc[fail]",
					new Object[] { this.getClass().getName() +"."+Thread.currentThread().getStackTrace()[1].getMethodName(),
						request,Arrays.deepToString(e.getStackTrace()) });
			throw new ServiceOperationException("请求处理失败：" + e.getMessage());
		}
		return response;
	}
	
}
